package com.ruoyi.cms.controller;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.cms.domain.*;
import com.ruoyi.cms.domain.vo.*;
import com.ruoyi.cms.service.*;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import static com.ruoyi.common.utils.PageUtils.startPage;

@Controller
public class CmsWebController extends BaseController {
    @Autowired
    private ICmsSiteInfoService cmsSiteInfoService;
    @Autowired
    private ICmsSiteModuleService cmsSiteModuleService;
    @Autowired
    private ICmsSiteBannerService cmsSiteBannerService;

    @Autowired
    private ICmsSiteWebsitService cmsSiteWebsitService;

    @Autowired
    private ICmsSiteAppService cmsSiteAppService;

    @Autowired
    private ICmsSiteModule4Service cmsSiteModule4Service;

    @Autowired
    private ICmsSiteCaseService cmsSiteCaseService;

    @Autowired
    private ICmsSiteBanneService cmsSiteBanneService;

    @Autowired
    private ICmsSiteAboutService cmsSiteAboutService;

    @Autowired
    private ICmsSiteVisitorMessageService cmsSiteVisitorMessageService;


    private CmsSiteInfo handPubData(Model model) {
        CmsSiteInfo info = cmsSiteInfoService.selectUseCmsSiteInfo();
        CmsSiteModule head = cmsSiteModuleService.selectCmsSiteModuleTypeValue(info.getModuleType(), "m_head");
        CmsSiteModule footer = cmsSiteModuleService.selectCmsSiteModuleTypeValue(info.getModuleType(), "m_footer");
        model.addAttribute("info", info);
        model.addAttribute("head", head);
        model.addAttribute("footer", footer);
        return info;
    }

    @GetMapping("/index")
    public String index(Model model) {
        CmsSiteInfo info = handPubData(model);
        String moduleType = info.getModuleType();
        CmsSiteModule paramsModule = new CmsSiteModule();
        paramsModule.setType(moduleType);
        paramsModule.setStatus(Constants.SUCCESS);
        List<CmsSiteModule> modules = cmsSiteModuleService.selectCmsSiteModuleList(paramsModule);
        modules.sort(Comparator.comparing(CmsSiteModule::getSort));

        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        for (CmsSiteModule m : modules) {
            CmsSiteBanner paramBanner = new CmsSiteBanner();
            paramBanner.setModuleId(m.getId());
            paramBanner.setStatus(Constants.SUCCESS);
            List<CmsSiteBanner> banners = cmsSiteBannerService.selectCmsSiteBannerList(paramBanner);
            banners.sort(Comparator.comparing(CmsSiteBanner::getSort));
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("banners", banners);
            moduleInfos.add(r);
        }
        model.addAttribute("modules", moduleInfos);
        /*System.out.println(JSON.toJSON(modules));*/
        return "index";
    }

    @GetMapping("/cms_websit")
    public String websit(Model model) {
        CmsSiteInfo info = handPubData(model);

        List<CmsSiteWebsitVo> data = cmsSiteWebsitService.getAllInfo();

        model.addAttribute(info);
        model.addAttribute("data", data);

        /*System.out.println(JSON.toJSON(data));*/
        return "cms_websit";
    }

    @GetMapping("/cms_app")
    public String app(Model model) {
        CmsSiteInfo info = handPubData(model);
        List<CmsSiteAppVo> data = cmsSiteAppService.getAllInfo();

        model.addAttribute(info);
        model.addAttribute("data", data);
       /* System.out.println(JSON.toJSON(data));*/
        return "cms_app";
    }

    @GetMapping("/cms_applet")
    public String applet(Model model) {
        CmsSiteInfo info = handPubData(model);
        List<CmsSiteModule4Vo> data = cmsSiteModule4Service.getAllInfo();
        model.addAttribute(info);
        model.addAttribute("data", data);
        /*System.out.println(JSON.toJSON(data));*/
        return "cms_applet";
    }

    @GetMapping("/cms_case")
    public String case1(Model model,@RequestParam(value = "pageSize",defaultValue = "6")Integer pageSize,
                        @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum) {
        CmsSiteInfo info = handPubData(model);
        System.out.println(pageSize);
        PageUtils.startPage(pageNum,pageSize);
        Long itemId = Convert.toLong(ServletUtils.getParameter("itemId"));
        CmsSiteBanne param = new CmsSiteBanne();
        if (itemId != null && itemId != 0) {
            param.setItemId(itemId);
        }
        PageUtils.orderBy("sort desc,id desc");
        List<CmsSiteBanne> data = cmsSiteBanneService.selectCmsSiteBanneList(param);
        List<CmsSiteCase> data1 = cmsSiteCaseService.selectCmsSiteCaseList(null);
        TableDataInfo td=getDataTable(data);
        model.addAttribute("data",td );
        model.addAttribute("data1",data1);
        System.out.println(JSON.toJSON(td));
        System.out.println(JSON.toJSON(data1));
        return "cms_case";
    }
   /* @GetMapping("/cms_case")
    public String case1(Model model, @RequestParam(value = "page", defaultValue = "1") String page) {
        CmsSiteInfo info = handPubData(model);
        List<CmsSiteCaseVo> data = cmsSiteCaseService.getAllInfo(page);
        model.addAttribute(info);
        model.addAttribute("data", data);

        *//*System.out.println(JSON.toJSON(data));*//*
        return "cms_case";
    }*/
    @GetMapping("/cms_about")
    public String about(Model model) {
        CmsSiteInfo info = handPubData(model);
        List<CmsSiteAboutVo> data = cmsSiteAboutService.getAllInfo();
        model.addAttribute(info);
        model.addAttribute("data",data);
        /*System.out.println(JSON.toJSON(data));*/
        return "cms_about";
    }

    @GetMapping("/cms_casedetail")
    public String casedetail(Model model) {
        CmsSiteInfo info = handPubData(model);


        return "cms_casedetail";
    }



    /**
     * 获取文章的分页信息
     *
     * @return
     */
    @GetMapping("/cms_articles")
    @ResponseBody
    public TableDataInfo articlePage(@RequestParam(value = "pageSize",defaultValue = "6")Integer pageSize,
                                     @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum){

        PageUtils.startPage(pageNum,pageSize);
        Long itemId = Convert.toLong(ServletUtils.getParameter("itemId"));
        CmsSiteBanne param = new CmsSiteBanne();
        if (itemId != null && itemId != 0) {
            param.setItemId(itemId);
        }
        PageUtils.orderBy("sort desc,id desc");
        List<CmsSiteBanne> data = cmsSiteBanneService.selectCmsSiteBanneList(param);
        TableDataInfo td=getDataTable(data);
        return td;
    }

   /* *//*表单提交*//*
    @PostMapping("/cms_message")
    @ResponseBody
    public AjaxResult message() {
//        message.setStatus("0");
        System.out.println("11111111111111");
//        System.out.println(message);
//        message.setCreateTime(DateUtils.getNowDate());
//        message.setHandRemark("");
        return toAjax(cmsSiteVisitorMessageService.insertCmsSiteVisitorMessage(null));
    }*/

    @GetMapping("/cms_message")
    @ResponseBody
    public int message(CmsSiteVisitorMessage cmsSiteVisitorMessage){
        System.out.println("!111111111");
        System.out.println(cmsSiteVisitorMessage);
        return cmsSiteVisitorMessageService.insertCmsSiteVisitorMessage(cmsSiteVisitorMessage);
    }

}
